Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
lazy-cache
Advanced tools
The lazy-cache npm package is designed to lazily require modules, meaning that the modules are only required when they are actually needed. This can help improve the performance of your application by deferring the loading of modules until they are used.
Lazy Loading of Modules
This feature allows you to defer the loading of a module until it is actually used in your code. In the example, the 'fs' module is not loaded until the 'readFileSync' method is called.
const lazy = require('lazy-cache')(require);
const fs = lazy('fs');
// fs is not loaded until it is used
fs.readFileSync('path/to/file.txt', 'utf8');
Custom Lazy Loading
You can also use lazy-cache to lazily load your own custom modules. In this example, the './customModule' is not loaded until 'someFunction' is called.
const lazy = require('lazy-cache')(require);
const customModule = lazy('./customModule');
// customModule is not loaded until it is used
customModule.someFunction();
The require-lazy package provides similar functionality by allowing you to lazily require modules. It offers a simple API for deferring the loading of modules until they are needed, similar to lazy-cache.
Proxyquire is a powerful tool for mocking dependencies during testing, but it also supports lazy loading of modules. It allows you to mock out dependencies and control when they are loaded, providing more flexibility compared to lazy-cache.
Import-lazy is another package that allows you to lazily import modules. It uses ES6 proxies to defer the loading of modules until they are accessed, offering a modern alternative to lazy-cache.
Cache requires to be lazy-loaded when needed.
Install with npm:
$ npm install lazy-cache --save
If you use webpack and are experiencing issues, try using unlazy-loader, a webpack loader that fixes the bug that prevents webpack from working with native javascript getters.
var utils = require('lazy-cache')(require);
Use as a property on lazy
The module is also added as a property to the lazy
function
so it can be called without having to call a function first.
var utils = require('lazy-cache')(require);
// `npm install glob`
utils('glob');
// glob sync
console.log(utils.glob.sync('*.js'));
// glob async
utils.glob('*.js', function (err, files) {
console.log(files);
});
Use as a function
var utils = require('lazy-cache')(require);
var glob = utils('glob');
// `glob` is a now a function that may be called when needed
glob().sync('foo/*.js');
An alias may be passed as the second argument if you don't want to use the automatically camel-cased variable name.
Example
var utils = require('lazy-cache')(require);
// alias `ansi-yellow` as `yellow`
utils('ansi-yellow', 'yellow');
console.log(utils.yellow('foo'));
Example
var utils = require('lazy-cache')(require);
// temporarily re-assign `require` to trick browserify
var fn = require;
require = utils;
// list module dependencies (here, `require` is actually `lazy-cache`)
require('glob');
require = fn; // restore the native `require` function
/**
* Now you can use glob with the `utils.glob` variable
*/
// sync
console.log(utils.glob.sync('*.js'));
// async
utils.glob('*.js', function (err, files) {
console.log(files.join('\n'));
});
In certain rare edge cases it may be necessary to unlazy all lazy-cached dependencies (5 reported cases after ~30 million downloads).
To force lazy-cache to immediately invoke all dependencies, do:
process.env.UNLAZY = true;
You might also be interested in these projects:
lint-deps: CLI tool that tells you when dependencies are missing from package.json and offers you a… more | homepage
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on April 22, 2016.
FAQs
Cache requires to be lazy-loaded when needed.
The npm package lazy-cache receives a total of 3,891,382 weekly downloads. As such, lazy-cache popularity was classified as popular.
We found that lazy-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.